<template>
	<view >
		<custom-navigation-bar title="商户管理"></custom-navigation-bar> 
		<view class="top01" > 
			<view class="top02" style="color: white;">
			&nbsp;&nbsp;&nbsp;&nbsp;共{{nums}}条数据
				<view class="top03">
					<image src="/static/ic_home_search@3x.png" class="top04" @click="showModal"></image>
				</view>
			</view>  
		</view> 
		<view class="top05"></view> 
		<view v-if="modalVisible" class="search">
		    <view class="search_1" @click="closeModal"></view>
			<view class="search_box"> 
				<view class="search_tiem">
					<view class="search_lable">名称</view>
					<view class="search_t"><input type="text" v-model="searchDate.shopName" class="search_input" placeholder="请输入名称"/> </view>
				</view> 
				 
				<view class="search_tiem">
					<view class="search_lable">状态</view>
					<view class="search_t">
						<view v-for="(item,index) in status" :key="index">
							<view class="search_def" v-if="item.id!=searchInitData.status" @click="selectStatus(item)">
								{{item.name}}
							</view>
							<view class="search_se" v-if="item.id==searchInitData.status" @click="selectStatus(item)">
								{{item.name}}
							</view>
						</view>
					</view>
				</view>
				<view class="search_tiem">
					<view class="search_lable">模块</view>
					<view class="search_t">
						<view v-for="(item,index) in moduleArr" :key="index">
							<view class="search_def" v-if="item.id!=searchInitData.module" @click="selectModule(item)">
								{{item.name}}
							</view>
							<view class="search_se" v-if="item.id==searchInitData.module" @click="selectModule(item)">
								{{item.name}}
							</view>
						</view>
					</view>
				</view>
				
				
				<view class="search_tiem" style="margin-bottom: 20rpx;">
					<view class="reset_btn" @click="reset">重置</view>
					<view class="ok_btn" @click="search">确定</view>
				</view>
		    </view>
		</view>
		<!-- 商户管理 -->
		<view v-if="data.length>0">
			<view class="list-item" v-for="(item,index) in data" :key="index" >
				<view class="list-shop-img" @click="detail(item)">
					<image :src="item.shopImage" class="list-shop-img1"></image>
				</view>
				<view class="list-item-content" @click="detail(item)" style="height: auto;">
					<view class="list-item-content-title">
						{{item.shopName}}
						<label v-if="item.status==0" class="status_def">待审核</label>
						<label v-if="item.status==1" class="status_cussess">正常</label>
						<label v-if="item.status==2" class="status_fail">未通过</label> 
					</view>
					<view class="list-item-content-v">
						地址：{{item.position}}
					</view>
					<view class="list-item-content-v">
						所属模块：{{item.moduleName}}
					</view>
					<view class="list-item-content-v">
						标签：{{item.tags}}
					</view>
				</view> 
				<view class="list-item-btn-box">
					<view class="list-item-btn" v-if="item.isHot==1" @click="uHotSet(item)">
						取消优选
					</view>
					<view class="list-item-btn" v-if="item.isHot==0" @click="hotSet(item)">
						设置优选
					</view> 
					<view class="list-item-btn" v-if="item.isHome==1" @click="setHome(item,0)">
						取消首页展示
					</view>
					<view class="list-item-btn" v-if="item.isHome==0&&item.isHot==1" @click="setHome(item,1)">
						首页展示
					</view>
				</view>
			</view>
			<view class="more" v-if="pageData.more" @click="getMore()">
			     加载更多
			</view>
			<view class="more" v-if="!pageData.more">
			     无更多数据
			</view>
		</view>
		<view class="more" v-if="data.length==0">
		     暂无数据
		</view>
	
		
		<view style="width: 100%;height: 60rpx;float: left;"></view>
		<uni-popup ref="popup" background-color="#fff">
			<view class="popup-content" :class="{ 'popup-height': type === 'left' || type === 'right' }">
				<view class="xieyi_title" style="background: #2AE8DC;">
					爆单设置
				<image src="../../static/close.png" class="xieyi_img" @click="closeToggle"></image>
				</view>
				<view class="xieyi_content" style="height: 320rpx;">
					<picker mode="date" @change="getTime">
						<view style="width: 80%;text-align: center;margin-top: 30rpx;border: 2rpx solid #666666;margin-left: 10%;line-height: 80rpx;font-size: 28rpx;">{{time}}</view>
					</picker>
					<input type="text" v-model="hotMessage" placeholder="请输入推荐原因" style="line-height: 80rpx;border: #666666 3rpx solid;margin-top: 20rpx;width: 80%;margin-left: 10%;height: 80rpx;"/>
					<view style="width: 100%;height:30rpx;float: left;"></view>
					<view class="reset_btn" @click="closeToggle">关闭</view>
					<view class="ok_btn" @click="setHot(1)">确定</view>
				 </view>
			</view>
		</uni-popup>
	</view>
</template>

<script setup>
	import CustomNavigationBar from '@/components/CustomNavigationBar.vue'; 
	import { ref,getCurrentInstance } from "vue"; 
	const {
		ctx
	} = getCurrentInstance();
	 var goodsIndex=ref();
	 //关闭协议
	 function closeToggle() {
		time.value="请选择失效时间"
	 	ctx.$refs.popup.close();
	 }
	 var time=ref("请选择失效时间");
	 function getTime(e){ 
	 	time.value=e.detail.value;
	 }
	function hotSet(item){
		goodsIndex.value=item,
		hotMessage.value="";
		ctx.$refs.popup.open('center')
	} 
	function uHotSet(item){
		goodsIndex.value=item;
		setHot(0);
	}
	const hotMessage=ref("");
	function setHot(status){
		if(status==1&&(time.value=='请选择失效时间'||hotMessage.value=='')){
			uni.showToast({
				icon: 'error',
				title: '请选择失效时间'
			})
			return;
		}
		uni.showLoading({
			title: '数据加载中',
			mask:true
		});  
		http.request({
		 	url: "/x/partner/home/shop/hot/set",
		 	method: "POST",
		 	data: {
				id:id.value,
				userId:userId.value,
				status:status,
				shop:{
					id:goodsIndex.value.id,
					isHotEndTime:time.value,
					hotMessage:hotMessage.value
				}
			}
		}).then((responseData) => { 
			uni.hideLoading();
			console.log(responseData)
			if(responseData.code==1){
				uni.showToast({
					icon: 'error',
					title: '成功'
				})
				closeToggle()
				initData();  
			}else if(responseData.code==0){
				uni.showToast({
					icon: 'error',
					title: responseData.message
				})
			}else if(responseData.code==-1){
				uni.navigateTo({
					url: "/pages/login/login"
				});
			}
		}).catch((err) => {
			uni.hideLoading();
		 	uni.showToast({
		 		icon: 'error',
		 		title: '请求失败'
		 	})
		}) 
	}
	
	function setHome(item,status){
		 
		uni.showLoading({
			title: '数据加载中',
			mask:true
		});  
		http.request({
		 	url: "/x/partner/home/shop/home/set",
		 	method: "POST",
		 	data: {
				id:id.value,
				userId:userId.value,
				status:status,
				shop:{
					id:item.id 
				}
			}
		}).then((responseData) => { 
			uni.hideLoading();
			console.log(responseData)
			if(responseData.code==1){
				uni.showToast({
					icon: 'error',
					title: '成功'
				})
				closeToggle()
				initData();  
			}else if(responseData.code==0){
				uni.showToast({
					icon: 'error',
					title: responseData.message
				})
			}else if(responseData.code==-1){
				uni.navigateTo({
					url: "/pages/login/login"
				});
			}
		}).catch((err) => {
			uni.hideLoading();
		 	uni.showToast({
		 		icon: 'error',
		 		title: '请求失败'
		 	})
		}) 
	}
	import http from '../../../utils/http';
	function detail(item){
		uni.navigateTo({
			url:"/pages/my/partner/shop_detail?shopId="+item.id+"&id="+id.value
		})
	}
	const modalVisible=ref(false); 
	function showModal() {
	    modalVisible.value = true;
	}
	function closeModal() {
	    modalVisible.value = false;
	}
	const searchInitData=ref({});
	const searchDate=ref({});
	const status=ref([
		{id:null,name:'全部'},
		{id:0,name:'待审核'},
		{id:1,name:'正常'},
		{id:2,name:'未通过'}
	]);
	 
	function selectStatus(item){
		searchInitData.value.status=item.id;
		searchDate.value.status=item.id;
	}
	function search(){
		closeModal();
		console.log(searchDate.value);
		pageData.value.pageIndex=1;
		initData();
	}
	function reset(){
		searchDate.value={} 
		searchInitData.value={}
		console.log(searchDate.value) 
	}
	  
	const id=ref(0);
	const userId=ref();
	import {
		onLoad,
		onPageScroll
	} from '@dcloudio/uni-app';
	onLoad((query) => {
		id.value=query.id;
		userId.value=getApp().globalData.user.userId
		//id.value=4;
		//userId.value=24;
		initData();
		module();
	})
	//初始化模块
	const moduleArr=ref([
		{
			id:null,
			name:'全部'
		}
	]);
	function module(){
		http.request({
			url: "/sys/menu/list",
			method: "POST",
			data: {
				"pid": 0,
				isShow:1
			}
		}).then((responseData) => { 
			 if(responseData.code==1){
				 //moduleArr.value=responseData.data;
				 for(var i=0;i<responseData.data.length;i++){
					 moduleArr.value.push(responseData.data[i])
				 }
			 }
		}).catch((err) => {
			uni.showToast({
				icon: 'error',
				title: '请求失败'
			})
		})
	}
	function selectModule(item){
		searchInitData.value.module=item.id;
		searchDate.value.module=item.id;
	}
	//----查询开始----
	const data=ref([]); 
	const pageData=ref(
		{
			pageIndex:1,
			pageSize:10,
			more:true
		}
	);
	function getMore(){
		pageData.value.pageIndex=pageData.value.pageIndex+1;
		initData();
	}
	const nums=ref(0);
	function initData(){
		uni.showLoading({
			title: '数据加载中',
			mask:true
		}); 
		http.request({
		 	url: "/x/partner/shop",
		 	method: "POST",
		 	data: {
		 		"id":id.value,
				userId:userId.value,
				pageIndex:pageData.value.pageIndex,
				pageSize:pageData.value.pageSize,
				shop:searchDate.value
		 	}
		}).then((responseData) => { 
			uni.hideLoading();
			if(responseData.code==1){
				nums.value=responseData.data.num;
				if(pageData.value.pageIndex==1){
					data.value=responseData.data.list;
				}else{
					if(responseData.data.list.length==0){
						pageData.value.more=false;
					}else{
						for(var i=0;i<responseData.data.list.length;i++){
							data.value.push(responseData.data.list[i])
						} 
					}
				}
			}else if(responseData.code==0){
				uni.showToast({
					icon: 'error',
					title: responseData.message
				})
			}else if(responseData.code==-1){
				uni.navigateTo({
					url: "/pages/login/login"
				});
			}
		}).catch((err) => {
			uni.hideLoading();
		 	uni.showToast({
		 		icon: 'error',
		 		title: '请求失败'
		 	})
		})
	}
	
	function pageIn(shop){
		//pageData.value.pageIndex=1;
		for(var i=0;i<data.value.length;i++){
			if(shop.id==data.value[i].id){
				data.value[i].status=shop.status
			}
		}
		 
	}
	defineExpose({
		pageIn
	}) 
	 
</script>

<style>
	@import "../../list/list.scss";
	@import "../../login/xieyi.scss";
	 .xiaoqu_item{
		 width: 90%;float: left;margin-left: 5%;background: white;margin-top: 20rpx;height: 80rpx;line-height: 80rpx;border-radius: 20rpx;
	 }
	 .more{
		 width: 100%;text-align: center;line-height: 50rpx;color: #666666;font-size: 25rpx;float: left;margin-top: 10rpx;
	 }
	 .edit{
		 float: right;color: dodgerblue;margin-right: 50rpx;
	 }
</style>